#include "evaluate.h"
#include "stdio.h"
FILE *fdebug;
void getMessage(char *msgbuf){
	int msglen;
	while(1){
		msglen = fscanf(stdin,"%s",msgbuf);
		//fprintf(fdebug,"Incoming : %s\n",msgbuf);
		if(msglen > 0)
			break;
	}
	return;
}
#define areEqual(string1, string2) ((strcmp(string1,string2)==0))
#define sendMessage(string) fprintf(stdout,string)
int playerid_of_computer=-1;
int resetboard_flag = -1;
int movenow = -1;

int handleMessage(char *msgbuf){
	int msglen;
	char tempbuf[10];
	msglen = strlen(msgbuf);
	//fprintf(fdebug,"%s %d\n",msgbuf,msglen);
	//playerid_of_computer = 1;
	if(msglen > 8){
		memcpy(tempbuf,msgbuf,8);
		tempbuf[8] = '\0';
	}
	if(msglen > 0){
		if(areEqual(msgbuf,"xboard")){
			//fprintf(fdebug,"reply to xboard");
			DEBUG_PRINT("incoming xboard, outgoing \\n\n");
			//sendMessage("\n");
		}else if(areEqual(msgbuf,"black")){
			//fprintf(fdebug,"computer is black\n");
			playerid_of_computer = 1;
			movenow = 0;
			DEBUG_PRINT("computer is black\n");
			//fprintf(fdebug,"plyerid %d movenow %d\n",playerid_of_computer,movenow);
		}else if(areEqual(msgbuf,"white")){
			playerid_of_computer = 0;
			movenow = 1;
			DEBUG_PRINT("computer is white\n");
		    //fprintf(fdebug,"plyerid %d movenow %d\n",playerid_of_computer,movenow);
			//fprintf(fdebug,"computer is white\n");
		}
		else if(areEqual(msgbuf,"protover")){
			//fprintf(fdebug,"reply to protover\n");
			DEBUG_PRINT("replying to protover\n");
			sendMessage("feature myname=\"alphaBeta1.0 \" playother=0 ping=0 setboard=0 playother=0 san=0 usermove=1 force=0 time=1 draw=0 sigint=0 sigterm=0 reuse=0 analyze=0 variants=\"normal\" colors=0 nps=0 debug=0 smp=0 done=1\n");
			sendMessage("done\n");
		}else if(areEqual(msgbuf,"new")){
			DEBUG_PRINT("incoming new \n");
			movenow = 0;
		}
		else if(areEqual(msgbuf,"quit"))
		{
			//fprintf(fdebug,"quit engine\n");
			DEBUG_PRINT("incoming quit\n");
			exit(1);
		}
		else if((msglen == 4))//&&(areEqual(tempbuf,"usermove")))
			return 1;
		
	}else
		return 0;
	return 0;
}
#define isalphabet(c) ((c>='a')&&(c<='h'))
#define isdigit(c) ((c >= '1')&&(c <='8'))
int parseMoveFromMsg(boardState *pbs,char *msgbuf,moveDesc *move){
	char *ptr;
	int startletter,endletter;
	int startnum1,startnum2,endnum1,endnum2;
	int startnum,endnum;
	int pieceidx;
	int msglen;
	int rookidx;
	int rook_oldpos,rook_newpos;
	int pieceType;
	
	ptr = msgbuf;
	msglen = strlen(msgbuf);
	if(msglen < 4)
		return 0;
	//fprintf(fdebug,"inside parsemove msg\n");
	startletter = *ptr++;
	if(!isalphabet(startletter)){
		//fprintf(fdebug,"not an alphabet\n");	
		return 0;
	}
	startletter -= 'a';
	startnum = *ptr++;
	if(!isdigit(startnum)){
		//fprintf(fdebug,"not a number\n");		
		return 0;
	}
	startnum -= '1';
	endletter = *ptr++;
	if(!isalphabet(endletter)){
		//fprintf(fdebug,"not a letter\n");		
		return 0;
	}
	endletter -= 'a';
	endnum = *ptr;// - '1';
	if(!isdigit(endnum)){
		//fprintf(fdebug,"not a number\n");	
		return 0;
	}
	endnum -= '1';
	DEBUG_PRINT("incoming move %s\n",msgbuf);
	move->oldpos = 8*startnum + (7-startletter);
	move->pieceidx = pbs->board[move->oldpos];
	move->newpos = 8*endnum + (7-endletter);
	move->validate = 0;
	move->pieceidx2 = -1;
	if(move->pieceidx != -1){
		pieceType = pbs->pieces[move->pieceidx].piecetype;
	}else
		pieceType = -1;
	// handling castling
	if(pieceType == KING)
	{
		// check if this is a castling move
		// set how the rook shld move
		if(move->newpos - move->oldpos == -2){
			// short castle
			DEBUG_PRINT("we have a Short castle\n");
			rook_oldpos = move->newpos-1;
			rook_newpos = move->newpos+1;
			rookidx = pbs->board[rook_oldpos];
			DEBUG_PRINT("king oldpos %d newpos %d idx %d\n",move->oldpos,move->newpos,move->pieceidx);
			DEBUG_PRINT("rook oldpos %d newpos %d idx %d\n",rook_oldpos,rook_newpos,rookidx);
			//fprintf(fedbg,"king oldpos %d newpos %d idx %d\n",move->oldpos,move->newpos,move->pieceidx);
			//fprintf(fedbg,"rook oldpos %d newpos %d idx %d\n",rook_oldpos,rook_newpos,rookidx);
			move->pieceidx2 = rookidx;
			move->oldpos2 = rook_oldpos;
			move->newpos2 = rook_newpos;			
		}else if(move->newpos - move->oldpos == 2){
			// long castle
			DEBUG_PRINT("we have a long castle\n");
			rook_oldpos = move->newpos+2;
			rook_newpos = move->newpos-1;
			rookidx = pbs->board[rook_oldpos];
			DEBUG_PRINT("king oldpos %d newpos %d idx %d\n",move->oldpos,move->newpos,move->pieceidx);
			DEBUG_PRINT("rook oldpos %d newpos %d idx %d\n",rook_oldpos,rook_newpos,rookidx);
			move->pieceidx2 = rookidx;
			move->oldpos2 = rook_oldpos;
			move->newpos2 = rook_newpos;			
		}else{
			// no castling is involved... nothing to do
		}
	}
	//fprintf(fdebug,"%s is a valid move\n");
	return 1;	
}
void sendMove(moveDesc *pMove){
	char msgbuf[5];
	int oldpos,newpos;
	char letter,number;
	oldpos = pMove->oldpos;
	newpos = pMove->newpos;
	letter = 'a' + (7-(oldpos%8));
	number = (oldpos/8);
	msgbuf[0] = letter;
	msgbuf[1] = '1' + number; 
	letter = 'a' + (7-(newpos%8));
	number = (newpos/8);
	msgbuf[2] = letter;
	msgbuf[3] = '1' + number;	
	msgbuf[4] = '\0';
	//fprintf(fdebug,"*********sent move %s\n",msgbuf);
	fprintf(stdout,"move %s\n",msgbuf);
	fflush(stdout);
	return;
}
	
